//
//  SystemUser.m
//  CZBaseFramework
//
//  Created by wulanzhou-mini on 15-5-22.
//  Copyright (c) 2015年 wulanzhou-mini. All rights reserved.
//

#import "SystemUser.h"
#import "Md5Encrypt.h"
#import "CacheDataUtil.h"
#import "NSDate+CZExtend.h"

#define kCFSystemUserCacheKey   @"kCFSystemUserCacheKey"
#define kCFSystemLoginDateKey   @"login_%@_date"


@implementation SystemUser

//判断是否为第一次运行
+ (BOOL)isFirstRun{
    if ([CacheDataUtil valueForKey:@"isAppFirstLoad"]) {
        return NO;
    }
    return YES;
}

//设置完成第一次运行
+ (void)setFinisheFirstRun{
    [CacheDataUtil setValue:@"1" forKey:@"isAppFirstLoad"];
}




- (id)init{
    if (self=[super init]) {
      
        self.userId=@"";
        self.phone=@"";
        self.password=@"";
        self.access_token=@"";
        self.refresh_token=@"";
        self.expires_in=@"";
        self.isLogin=NO;
    }
    return self;
}

+ (SystemUser*)shareInstance{
    SystemUser *config=[CacheDataUtil unarchiveValueForKey:kCFSystemUserCacheKey];
    if (config==nil) {
         config=[[SystemUser alloc] init];
    }
    return config;
}

/**
 *  字典转换为对象
 *  @param dic 登录成功后的字典内容
 *  @reutrn    字典转对象
 */
- (id)initWithDictionary:(NSDictionary*)dic{
    if (self=[super init]) {
        if (dic&&[dic isKindOfClass:[NSDictionary class]]&&[dic count]>0) {
            
            if ([dic.allKeys containsObject:@"id"]) {
                self.userId=[NSString stringWithFormat:@"%@",[dic objectForKey:@"id"]];
            }
            
            if ([dic.allKeys containsObject:@"access_token"]){
                self.access_token=[dic objectForKey:@"access_token"];
            }
            
            if ([dic.allKeys containsObject:@"expires_in"]) {
                self.expires_in=[NSString stringWithFormat:@"%@",[dic objectForKey:@"expires_in"]];
            }
            if ([dic.allKeys containsObject:@"refresh_token"]){
                self.refresh_token=[dic objectForKey:@"refresh_token"];
            }
            
            
            //保存过期时间
            [self saveExpiresDate];

            self.isLogin=YES;
            
        }
    }
    return self;
}

/**
 *  保存token访问时间
 */
- (void)saveExpiresDate{
    if (self.expires_in&&[self.expires_in length]>0) {
        NSDate *loginDate=[[NSDate date] dateByAddingMinutes:[self.expires_in integerValue]/60.0];
        //保存当前登录时间
        [CacheDataUtil setValue:loginDate forKey:[NSString stringWithFormat:kCFSystemLoginDateKey,self.userId]];
    }

}

/**
 *  判断access_token是否已过期
 *
 *  @return YES:表示过期 NO:表示未过期
 */
- (BOOL)isExpiresAccessToken{
    NSString *key=[NSString stringWithFormat:kCFSystemLoginDateKey,self.userId];
    NSDate *loginDate=[CacheDataUtil valueForKey:key];
    if (loginDate) {
        
        if ([loginDate compare:[NSDate date]]==NSOrderedAscending) {
            return YES;
        }
    }
    return NO;
}

/**
 *  uid与token请求参数
 *
 *  @return   返回请求参数
 */
- (NSDictionary*)GetRequestParamsWithUidAndToken{
    return [NSDictionary dictionaryWithObjectsAndKeys:self.userId,@"id",self.access_token,@"token", nil];
}

/**
 *  uid与pass请求参数
 *
 *  @return   返回请求参数
 */
- (NSDictionary*)GetRequestParamsWithUidAndPassword{
   return [NSDictionary dictionaryWithObjectsAndKeys:self.userId,@"id",[Md5Encrypt md5:self.password],@"pass", nil];
}

/**
 *  用户信息保存
 */
- (void)saveUser{    
    [CacheDataUtil setValueArchiver:self forKey:kCFSystemUserCacheKey];
}

/**
 *  用户信息删除
 */
- (void)removeUser{
    [CacheDataUtil removeForKey:kCFSystemUserCacheKey];
}

@end
